package com.ddh.codeduck.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codeduck.pojo.dto.CodeDuckUserDTO;
import com.codeduck.pojo.dto.CodeDuckUserPwdDTO;
import com.codeduck.pojo.entity.CodeDuckUser;
import com.codeduck.pojo.req.CodeDuckUserPageReq;
import com.ddh.codeduck.service.ICodeDuckUserService;
import com.ddhcodeduck.common.result.BaseResponse;
import com.ddhcodeduck.common.result.ResultUtils;
import com.ddhcodeduck.common.utils.PasswordUtil;
import com.codeduck.pojo.vo.CodeDuckUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ddh
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/codeDuckUser")
@Api(tags = "用户信息")
public class CodeDuckUserController {

    @Autowired
    private ICodeDuckUserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<Boolean> register(@RequestBody CodeDuckUserDTO cardUserRegisterDTO) {
        boolean register = userService.register(cardUserRegisterDTO);
        return ResultUtils.success(register);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<CodeDuckUserVO> login(@RequestBody CodeDuckUserDTO cardUserRegisterDTO) {
        CodeDuckUserVO cardUserLoginVO = userService.login(cardUserRegisterDTO);
        return ResultUtils.success(cardUserLoginVO);
    }

    /**
     * 查询所有用户
     */
    @PostMapping("/list")
    @ApiOperation("查询所有用户")
    public BaseResponse<IPage<CodeDuckUserVO>> list(@RequestBody CodeDuckUserPageReq codeDuckUserPageReq) {
        IPage<CodeDuckUserVO> page = userService.listUser(codeDuckUserPageReq);
        return ResultUtils.success(page);
    }

    /**
     * 新增或修改用户
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改用户")
    public BaseResponse<Boolean> saveOrUpdate(@RequestBody CodeDuckUser codeDuckUser) {
        return ResultUtils.success(userService.mySaveOrUpdate(codeDuckUser));
    }

    /**
     * 根据id查询用户
     */
    @PostMapping("/getById")
    @ApiOperation("根据id查询用户")
    public BaseResponse<CodeDuckUser> getById(@RequestParam("id") String id) {
        CodeDuckUser byId = userService.getById(id);
        String password = byId.getPassword();
        //由于密码加密不可逆，置空密码返回前端
        byId.setPassword(null);
        byId.setPassword(PasswordUtil.encode(password));
        return ResultUtils.success(userService.getById(id));
    }

    /**
     * 批量删除用户
     */
    @PostMapping("/removeBatchByIds")
    @ApiOperation("批量删除用户")
    public BaseResponse<Boolean> removeBatchByIds(@RequestBody List<String> ids) {
        return ResultUtils.success(userService.removeBatchByIds(ids));
    }

    /**
     * 根据token去查询用户
     */
    @PostMapping("/getByToken")
    @ApiOperation("根据token去查询用户")
    public BaseResponse<CodeDuckUserVO> getByToken() {
        CodeDuckUserVO cardUserVO = userService.getByToken();
        return ResultUtils.success(cardUserVO);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public BaseResponse<Boolean> logout() {
        return ResultUtils.success(userService.logout());
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPassword")
    @ApiOperation("重置密码")
    public BaseResponse<Boolean> resetPassword(@RequestBody CodeDuckUserPwdDTO codeDuckUserPwdDTO) {
        return ResultUtils.success(userService.resetPassword(codeDuckUserPwdDTO));
    }
}
